package com.yonyougov.devops.cruxcisupport.button;

import com.yonyougov.devops.cruxcisupport.button.vo.ButtonByMenuIdRespVo;
import com.yonyougov.devops.cruxcisupport.button.vo.CurrentUserButtonByMenuCodeDto;
import com.yonyougov.devops.cruxcisupport.button.vo.NoLinkButtonByRoleIdDto;
import com.yonyougov.devops.cruxcisupport.button.vo.NoLinkButtonByRoleIdVo;
import com.yonyougov.devops.cruxcisupport.role.RoleService;
import com.yonyougov.devops.cruxcisupport.rolebutton.RoleButtonEntity;
import com.yonyougov.devops.cruxcisupport.rolebutton.RoleButtonService;
import com.yonyougov.devops.fwc.core.dao.BaseDao;
import com.yonyougov.devops.fwc.core.entity.Pagination;
import com.yonyougov.devops.fwc.core.service.impl.BaseServiceImpl;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.List;

/**
 * t_button服务实现
 */
@Service
public class ButtonServiceImpl extends BaseServiceImpl<ButtonEntity, BigInteger> implements ButtonService {
    private ButtonDao buttonDao;
    @Autowired
    private RoleButtonService roleButtonService;
    @Autowired
    private RoleService roleService;

    ButtonServiceImpl(ButtonDao buttonDao) {
        this.buttonDao = buttonDao;
    }

    @Override
    public BaseDao<ButtonEntity, BigInteger> getDao() {
        return buttonDao;
    }


    //<!--用户自定义代码开始-->
    @Override
    public Pagination<NoLinkButtonByRoleIdVo> findNoLinkButtonByRoleId(NoLinkButtonByRoleIdDto noLinkButtonByRoleIdDto) {
        List<NoLinkButtonByRoleIdVo> noLinkButtonByRoleId = buttonDao.findNoLinkButtonByRoleId(noLinkButtonByRoleIdDto);
        int totalCount = buttonDao.countNoLinkButtonByRoleId(noLinkButtonByRoleIdDto);

        return new Pagination<>(noLinkButtonByRoleIdDto.getMax(), noLinkButtonByRoleIdDto.getPageNum(), totalCount, noLinkButtonByRoleId);
    }

    @SneakyThrows
    @Override
    public List<ButtonByMenuIdRespVo> findCurrentUserButtonByMenuCode(CurrentUserButtonByMenuCodeDto currentUserButtonByMenuIdDto) {
        currentUserButtonByMenuIdDto.setRoleIds(roleService.getCurrentUserRoleIds());
        return buttonDao.findButtonByMenuCode(currentUserButtonByMenuIdDto);
    }

    @Override
    public void deleteOneButtonByButtonId(BigInteger buttonId) {
        // 删除按钮
        deleteByPrimaryKey(buttonId);
        // 删除角色按钮
        roleButtonService.delete(RoleButtonEntity.builder().btnId(buttonId).build());
    }

    //<!--用户自定义代码结束-->
}
